
import NProgress from 'nprogress' // 进度条 
import router from './router'     //roter
import 'nprogress/nprogress.css' // 进度条  style
import {getToken} from '@/utils/tokin'
import store from './store'
// import getPageTitle from '@/utils/get-page-title'
NProgress.configure({ showSpinner: false }) //配置进度条

// 获取页面的路由权限
var tonken= getToken('FcLogin');
if(tonken){
  if(JSON.parse(tonken).length>=1){
    let arr=JSON.parse(tonken)[1].split(',');
    console.log(tonken)
    // let  arr=[  '1','2','3', '4','5', '7', '8', '9', '10', '11'] 
      store.dispatch('user/generateRoutes', arr)
    store.getters.roters.forEach(item =>{
      router.addRoute(item) //添加动态路由
    })
  }
}
router.beforeEach(async (to, from, next) => {
  NProgress.start()
  // 开始进度条
  const  hasToken= getToken('FcLogin');
  if(hasToken){
    if (to.path === '/login') {
        next({ path: '/' })
        NProgress.done()
    }else{
      next();
      NProgress.done()
    }
  }else{
    if(to.path=='/login'){
      next()
    }else{
        next(`/login?redirect=${to.path}`)
        // next(`/login`)
    }
    NProgress.done()
  }
})
router.afterEach(() => {
  // 完成进度条
  NProgress.done()
})





